--[[
   all log thingies going on here
]]--

Optimizer.Log = {};
Optimizer.Log_mt = { __index = Optimizer.Log };

function Optimizer.Log:new()
    local new_inst = {
        Segments = {},
    };   -- the new instance
    setmetatable( new_inst, Optimizer.Log_mt ); -- all instances share the same metatable
    return new_inst;     
end

function Optimizer.Log:AddEntry(type,time,name,eff,str)
    if self.Segments[#self.Segments].active == true then
--~         DEFAULT_CHAT_FRAME:AddMessage(type..' '..time..' '..name..' '..eff..' '..str);
        table.insert(self.Segments[#self.Segments].entrys, {
            type = type,
            time = time,
            name = name,
            eff = eff,
            info = str
        });
    end
end

function Optimizer.Log:AddEff(spellname,eff)
    if self.Segments[#self.Segments].active == true then
        self.Segments[#self.Segments].spellEff[spellname].n = self.Segments[#self.Segments].spellEff[spellname].n + 1;
        self.Segments[#self.Segments].spellEff[spellname].total = self.Segments[#self.Segments].spellEff[spellname].total + eff;
    end
end

function Optimizer.Log:NewSegment(boss,starttime)
    table.insert(self.Segments, {
      start = starttime,
      stop = nil,
      boss = boss,
      active = true,
      entrys = {},
      spellEff = {},
        }
    );
    
    for i,v  in pairs(Optimizer.Player.spellLib) do        
         self.Segments[#self.Segments].spellEff[i] = {
                n = 0,
                total = 0,
                min = 0,
                max = 0
        };
    end
end

function Optimizer.Log:GetEntry()

end

function Optimizer.Log:GetSegment()

end

function Optimizer.Log:CloseSegment(time)
    self.Segments[#self.Segments].active = false;
    self.Segments[#self.Segments].stop = time;    
end

function Optimizer.Log:PrintSegment(segment)
    local timeInCombat = math.floor(segment.stop - segment.start);
    local curSpellEff = segment.spellEff;
    local totalSpellEff = 0;
    local nSpell = 0;
    local rangeEff,sightEff = 0,0;
    local totalEff = 0;
    local x,y = 0,0;
    
    DEFAULT_CHAT_FRAME:AddMessage('--- OptimizeMe ---');    
    DEFAULT_CHAT_FRAME:AddMessage('combat time: '..SecondsToTime(timeInCombat));
    
    msec = math.floor((Optimizer._timeWNStotal - math.floor(Optimizer._timeWNStotal))*1000);
    sightEff = ((timeInCombat - Optimizer._timeWNStotal) * 100) / timeInCombat;
    sightEff = self:Round(sightEff);
    DEFAULT_CHAT_FRAME:AddMessage('time out of sight: '..SecondsToTime(math.floor(Optimizer._timeWNStotal))..' '..msec..' mSek. : '..sightEff..'%');
    
    msec = math.floor((Optimizer._timeWNRtotal - math.floor(Optimizer._timeWNRtotal))*1000);
    rangeEff = ((timeInCombat - Optimizer._timeWNRtotal) * 100) / timeInCombat;
    rangeEff = self:Round(rangeEff);
    DEFAULT_CHAT_FRAME:AddMessage('time out of range: '..SecondsToTime(math.floor(Optimizer._timeWNRtotal))..' '..msec..' mSek. : '..rangeEff..'%');
    
    for i,v in pairs(curSpellEff) do        
        if v ~= nil then  
            local avg = 0;    
            
            if(v.n ~= 0) then
                avg = v.total / v.n;
                avg = self:Round(avg);
            end

            DEFAULT_CHAT_FRAME:AddMessage(i..' effizienz: '..avg..'%');
            
            totalSpellEff = totalSpellEff + avg;
            nSpell = nSpell + 1;
       end
    end
    
    if nSpell ~= 0 then
        totalSpellEff = totalSpellEff / nSpell;
    end
    
    totalEff = (totalSpellEff + rangeEff + sightEff) / 3;
    totalEff = self:Round(totalEff);
    DEFAULT_CHAT_FRAME:AddMessage('TOTAL EFF: '..totalEff..'%'); 
    DEFAULT_CHAT_FRAME:AddMessage('----------');  
end

function Optimizer.Log:Round(num)
    local x,y = math.modf(num);
    y = math.floor(y*10) / 10;
    x = x + y;
    return x;
end